<template>
  <div class="jsbos-common-layout">
    <div class="jsbos-common-layout-center">
      <el-row class="jsbos-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="4">
            <el-form-item label="模板类型">
              <el-select
                v-model="query.templateType"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="(item, index) in templateTypeOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.id"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="消息渠道">
              <el-select
                v-model="query.messageType"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="(item, index) in messageTypeOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.enCode"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="消息场景">
              <McBusinessDialog
                v-model="query.messageSource"
                :title="query.messageSourceName"
                @change="onMessageSourceChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="关键字">
              <el-input
                v-model="listQuery.keyword"
                placeholder="请输入"
                clearable
                @keyup.enter.native="search()"
              ></el-input>
            </el-form-item>
          </el-col>
          <template v-if="showAll">
            <!-- <el-col :span="4">
              <el-form-item label="名称">
                <el-input
                  v-model="query.fullName"
                  placeholder="请输入"
                  clearable
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="编码">
                <el-input
                  v-model="query.enCode"
                  placeholder="请输入"
                  clearable
                ></el-input>
              </el-form-item>
            </el-col> -->
            <el-col :span="4">
              <el-form-item label="订阅类型">
                <el-select
                  v-model="query.subscriberType"
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="(item, index) in subscriberTypeOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </template>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >重置</el-button
              >
              <el-button
                type="text"
                icon="el-icon-arrow-down"
                @click="showAll = true"
                v-if="!showAll"
              >
                展开
              </el-button>
              <el-button
                type="text"
                icon="el-icon-arrow-up"
                @click="showAll = false"
                v-else
              >
                收起
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="jsbos-common-layout-main jsbos-flex-main">
        <div class="jsbos-common-head">
          <div>
            <!-- v-has="'btn_add'" -->
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle()"
              >新增
            </el-button>
            <!-- v-has="'btn_download'" -->
            <!-- <el-button type="text" icon="el-icon-download" @click="exportData()"
              >导出
            </el-button> -->
          </div>
          <div class="jsbos-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JSBOS-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
          </div>
        </div>
        <jsbos-table
          v-loading="listLoading"
          :data="list"
          :border="true"
          style="margin-left: 5px"
          :isHeader="false"
          @sort-change="sortChange"
        >
          <el-table-column
            prop="fullName"
            label="名称"
            min-width="200"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="enCode"
            label="编码"
            min-width="200"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="模板类型"
            width="150"
            prop="templateType"
            algin="center"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              {{ scope.row.templateType | dynamicText(templateTypeOptions) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="messageSource"
            label="消息场景"
            width="150"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="messageType"
            label="消息渠道"
            width="150"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="sortCode"
            label="排序"
            width="50"
            :align="'center'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="enabledMark"
            label="状态"
            width="70"
            :align="'center'"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.enabledMark == 1 ? 'success' : 'danger'"
                disable-transitions
              >
                {{ scope.row.enabledMark == 1 ? "启用" : "禁用" }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="title"
            label="标题"
            min-width="150"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="content"
            label="内容"
            min-width="150"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="templateCode"
            label="模板编号"
            min-width="330"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="creatorTime"
            label="创建时间"
            width="180"
            :align="'center'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="creatorUserId"
            label="创建用户"
            width="100"
            :align="'center'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="lastModifyTime"
            label="修改时间"
            width="180"
            :align="'center'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="lastModifyUserId"
            label="修改用户"
            width="100"
            :align="'center'"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="操作" fixed="right" width="150">
            <template slot-scope="scope">
              <!-- v-has="'btn_edit'" -->
              <el-button type="text" @click="addOrUpdateHandle(scope.row.id)"
                >编辑
              </el-button>
              <!-- v-has="'btn_remove'" -->
              <el-button
                type="text"
                class="jsbos-table-delBtn"
                @click="handleDel(scope.row.id)"
                >删除
              </el-button>
              <!-- v-has="'btn_detail'" -->
              <!-- <el-button type="text" @click="goDetail(scope.row.id)"
                >详情
              </el-button> -->
            </template>
          </el-table-column>
        </jsbos-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <JSBOS-Form v-if="formVisible" ref="JSBOSForm" @refresh="refresh" />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
    <Detail
      v-if="detailVisible"
      ref="Detail"
      @refresh="detailVisible = false"
    />
  </div>
</template>

<script>
import { getList, Delete } from "@/views/message/api/mcMsgTemplate";
import request from "@/utils/request";
import JSBOSForm from "./Form";
import ExportBox from "./ExportBox";
import Detail from "./Detail";
import McBusinessDialog from "@/views/message/mcBusiness/mcBusinessDialog.vue";

export default {
  name: "mcMsgTemplate",
  components: { JSBOSForm, ExportBox, Detail, McBusinessDialog },
  data() {
    return {
      showAll: false,
      detailVisible: false,
      query: {
        templateType: undefined,
        messageType: undefined,
        messageSource: undefined,
        messageSourceName: undefined,
        fullName: undefined,
        enCode: undefined,
        subscriberType: undefined,
      },
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: "asc",
        sidx: "sortCode",
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [
        { prop: "fullName", label: "名称" },
        { prop: "enCode", label: "编码" },
        { prop: "templateType", label: "模板类型" },
        { prop: "messageSource", label: "消息场景" },
        { prop: "messageType", label: "消息渠道" },
        { prop: "sortCode", label: "排序" },
        { prop: "enabledMark", label: "状态" },
        { prop: "description", label: "说明" },
        { prop: "title", label: "标题" },
        { prop: "content", label: "内容" },
        { prop: "templateCode", label: "模板编号" },
        { prop: "wxSkip", label: "跳转方式" },
        { prop: "xcxAppId", label: "关联小程序ID" },
        { prop: "subscriberType", label: "订阅类型" },
        { prop: "creatorTime", label: "创建时间" },
        { prop: "creatorUserId", label: "创建用户" },
        { prop: "lastModifyTime", label: "修改时间" },
        { prop: "lastModifyUserId", label: "修改用户" },
      ],
      templateTypeOptions: [
        { fullName: "自定义模板", id: "0" },
        { fullName: "系统模板", id: "1" },
      ],
      templateTypeProps: {
        label: "fullName",
        value: "id",
      },
      messageTypeOptions: [],
      messageTypeProps: {
        label: "fullName",
        value: "enCode",
      },
      wxSkipOptions: [
        { fullName: "小程序", id: "1" },
        { fullName: "页面", id: "2" },
      ],
      wxSkipProps: {
        label: "fullName",
        value: "id",
      },
      subscriberTypeOptions: [
        { fullName: "一次性订阅", id: "1" },
        { fullName: "长期订阅", id: "2" },
      ],
      subscriberTypeProps: {
        label: "fullName",
        value: "id",
      },
    };
  },
  computed: {
    menuId() {
      return this.$route.meta.modelId || "";
    },
  },
  created() {
    this.initData();
    this.getMessageTypeOptions();
  },
  methods: {
    /**消息场景change事件 */
    onMessageSourceChange(id, item) {
      if (!id) return (this.query.messageSourceName = "");
      this.query.messageSourceName = item.fullName;
    },
    getMessageTypeOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "msgSendType",
        })
        .then((res) => {
          this.messageTypeOptions = res;
        });
    },
    goDetail(id) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.Detail.init(id);
      });
    },
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    initData() {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      getList(_query).then((res) => {
        var _list = [];
        for (let i = 0; i < res.data.list.length; i++) {
          let _data = res.data.list[i];
          _list.push(_data);
        }
        this.list = _list;
        this.total = res.data.pagination.total;

        this.listLoading = false;
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          Delete(id).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.JSBOSForm.init(id, isDetail);
      });
    },
    exportData() {
      this.exportBoxVisible = true;
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList);
      });
    },
    download(data) {
      let query = {
        ...data,
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      request({
        url: `/api/msgCenter/mcMsgTemplate/Actions/Export`,
        method: "GET",
        data: query,
      }).then((res) => {
        if (!res.data.url) return;
        this.jsbos.downloadFile(res.data.url);
        this.$refs.ExportBox.visible = false;
        this.exportBoxVisible = false;
      });
    },
    search() {
      this.initData();
    },
    refresh(isInitData) {
      this.formVisible = false;
      if (isInitData) this.initData();
    },
    reset() {
      this.resetQuery();
      this.resetListQuery();
      this.initData();
    },
    resetQuery() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
    },
    resetListQuery() {
      this.listQuery = {
        keyword: "",
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatorTime",
      };
    },
  },
};
</script>
